package com.clouds.wms.pur;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.clouds.wms.bean.Cmsmd;
import com.clouds.wms.bean.Cmsmk;
import com.clouds.wms.bean.Purma;
import com.clouds.wms.bean.Purmg;
import com.clouds.wms.exception.SysException;
import com.clouds.wms.service.CmsmjService;
import com.clouds.wms.service.CmsmkService;
import com.clouds.wms.service.PurmgService;
import com.clouds.wms.service.PurmdService;
import com.clouds.wms.service.PurmgService;
import com.clouds.wms.util.Function;
import com.clouds.wms.util.PagesBean;
import com.clouds.wms.util.RequestHelper;
import com.clouds.wms.util.SysParams;

@Controller
@RequestMapping("/purmg")
public class PurmgController {
	private Log log = LogFactory.getLog(getClass());
	@Resource(name="purmgService")
	private PurmgService purmgService;
	@Resource(name="purmdService")
	private PurmdService purmdService;
	@Resource(name="cmsmjService")
	private CmsmjService cmsmjService;
	@Resource(name="cmsmkService")
	private CmsmkService cmsmkService;
	@RequestMapping(value = "/purmgMain.xhtml")
	public String purmgMain(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		model.addAttribute("allCmsmjList", cmsmjService.getCmsmjListByMjac(SysParams.DOCUMENT_CATEGORY_CGSH));
		return "pur/purmg";
	}
	@RequestMapping(value = "/purmgList.xhtml")
	public String purmgList(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String mgaa = RequestHelper.getString(request, "mgaa");
		String mgab = RequestHelper.getString(request, "mgab");
		String mgac = RequestHelper.getString(request, "mgac");
		String startMgad = RequestHelper.getString(request, "startMgad");
		String endMgad = RequestHelper.getString(request, "endMgad");
		int pageNumber = RequestHelper.getInt(request, "page", 1);// 要跳转的页号
		int pageSize = RequestHelper.getInt(request, "rows", 10);// 一页的条数
		PagesBean pb = purmgService.getPurmgList(mgaa, mgab, mgac, startMgad, endMgad, pageNumber, pageSize);
		List<Purmg> list = (List<Purmg>) pb.getData();
		int count = pb.getCount();
		Map<String,Object> paraMap = new HashMap<String,Object>();
		paraMap.put("total", count);
		paraMap.put("rows", list);
		try {
			Function.toJson(response, paraMap);
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		return null;
	}
	/**
	 * 添加采购单单头
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/addPurmg.xhtml", method = RequestMethod.POST)
	public String addPurma(HttpServletRequest request, HttpServletResponse response, Model model, Purmg purmg) throws SysException, IOException {
		String mgaa = purmg.getMgaa();
		String mgab = purmg.getMgab();
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		Purmg purmgtmp = purmgService.getPurmg(mgaa,mgab);
		if(purmgtmp != null && purmgtmp.getMgaa() != null && !"".equals(purmgtmp.getMgaa())) {
			out.print("该收货单的编号已经存在，请重新录入!");
			return null;
		}
		purmg.setMgad(Function.getDateString(new Date()));
		purmgService.savePurmg(purmg);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("增加了编号为[" + mgab + "]的收货单!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	
}
